using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace SchEdu.Framework.DataModel
{


    public class OrderList : List<SchEduDataSet.OrderRow>
    {
        public OrderList():base()
        {
        }

        public OrderList(OrderList orders)
            :base(orders)
        {
        }

        public OrderList(SchEduDataSet.OrderRow[] orders)
            : base(orders)
        {
        }

        public OrderList(SchEduDataSet.OrderDataTable orders)
        {
            foreach(SchEduDataSet.OrderRow order in orders)
            {
                Add(order);
            }
        }


        public SchEduDataSet.OrderRow FindByID(int ID)
        {
            foreach (SchEduDataSet.OrderRow order in this)
            {
                if (ID == order.ID)
                {
                    return order;
                }
            }
            return null;
        }

        public SchEduDataSet.OrderRow FindByGuid(System.Guid Guid)
        {
            foreach (SchEduDataSet.OrderRow order in this)
            {
                if (Guid == order.Guid)
                {
                    return order;
                }
            }
            return null;
        }

        public int GetMakespan()
        {
            int endDate = 0;
            foreach (SchEduDataSet.OrderRow order in this)
            {
                endDate = Math.Max(endDate, order.EndDate);
            }
            return endDate;
        }

        public SchEduDataSet.OrderRow MaxProcessingTime()
        {
            SchEduDataSet.OrderRow maxProcessingTime = null;
            foreach (SchEduDataSet.OrderRow order in this)
            {
                if (maxProcessingTime == null)
                {
                    maxProcessingTime = order;
                }
                if (order.ProcessingTime > maxProcessingTime.ProcessingTime)
                {
                    maxProcessingTime = order;
                }
            }
            return maxProcessingTime;
        }

        public int ProcessingTime()
        {
            int processingTime = 0;
            foreach (SchEduDataSet.OrderRow order in this)
            {
                processingTime += order.ProcessingTime;
            }
            return processingTime;
        }

        public void SortBySPT()
        {
            Sort(new SPTComparer());
        }

        public void SortByLPT()
        {
            Sort(new LPTComparer());
        }

        public void SortByWSPT()
        {
            Sort(new WSPTComparer());
        }

        public void SortByEDD()
        {
            Sort(new EDDComparer());
        }



        class SPTComparer : Comparer<SchEduDataSet.OrderRow>
        {

            public override int Compare(SchEduDataSet.OrderRow x, SchEduDataSet.OrderRow y)
            {
                if (x.ProcessingTime < y.ProcessingTime)
                {
                    return -1;
                }
                if (x.ProcessingTime > y.ProcessingTime)
                {
                    return 1;
                }
                return 0;
            }
        }

        class EDDComparer : Comparer<SchEduDataSet.OrderRow>
        {

            public override int Compare(SchEduDataSet.OrderRow x, SchEduDataSet.OrderRow y)
            {
                if (x.DueDate < y.DueDate)
                {
                    return -1;
                }
                if (x.DueDate > y.DueDate)
                {
                    return 1;
                }
                return 0;
            }
        }

        class LPTComparer : Comparer<SchEduDataSet.OrderRow>
        {

            public override int Compare(SchEduDataSet.OrderRow x, SchEduDataSet.OrderRow y)
            {
                if (x.ProcessingTime > y.ProcessingTime)
                {
                    return -1;
                }
                if (x.ProcessingTime < y.ProcessingTime)
                {
                    return 1;
                }
                return 0;
            }
        }

        class WSPTComparer : Comparer<SchEduDataSet.OrderRow>
        {

            public override int Compare(SchEduDataSet.OrderRow x, SchEduDataSet.OrderRow y)
            {
                if (x.Weight / x.ProcessingTime > y.Weight / y.ProcessingTime)
                {
                    return -1;
                }
                if (x.Weight / x.ProcessingTime < y.Weight / y.ProcessingTime)
                {
                    return 1;
                }
                return 0;
            }
        }

    }



}
